﻿@model TestOnlineProject.Models.Entities.Test
@using TestOnlineProject.Models.Entities
@using TestOnlineProject.Common
@{
    ViewBag.Title = "Details";
}
@{
    if (!String.IsNullOrEmpty(ViewBag.Msg))
    {
    <script>        alert("@ViewBag.Msg")</script>
    }
}
<script>
    $(document).ready(function () {
        $("#deactivate").click(function () {
            var con = confirm("Are you sure you want to deactivate this test?");
            if (con == true) {
                $.ajax({
                    url: "@Url.Content("~/Test/Deactivate")" + "?ids=" + @Model.TestId + "-",
                    type: 'GET',
                    success: function (data) {
                        if (data != null) {
                            alert(data);
                            window.location="@Url.Content("~/Test/Details")" + "?id=" + @Model.TestId;
                        }
                    }
                })
            }
        }); $("#activate").click(function () {
            var con = confirm("Are you sure you want to activate this test?");
            if (con == true) {
                $.ajax({
                    url: "@Url.Content("~/Test/Activate")" + "?ids=" + @Model.TestId + "-",
                    type: 'GET',
                    success: function (data) {
                        if (data != null) {
                            alert(data);
                            window.location="@Url.Content("~/Test/Details")" + "?id=" + @Model.TestId;
                        }
                    }
                })
            }
        });
    })

    // Set test id.
    var testId = 0;
    function SetTestId(t) {
        testId = t;
    }
    $(document).ready(function () {
        // Check existent test session.
        $(".take_the_test").click(function () {
            $.ajax({
                url: "@Url.Content("~/Test/CheckUnfinishedTest")",
                type: 'GET',
                success: function (data) {
                    if (data > 0) {
                        var con = confirm("Would you want to continue the unfinished test?");
                        if (con == true) {
                        var url="";
                        if(@Model.TestTypeId == 1)
                        {
                            url = "@Url.Content("~/Test/Test")?userTestId=" + data;
                        }
                            win = window.open(url, '_blank');
                            win.focus();
                        }
                        else {
                            var url = "@Url.Content("~/Test/TakeTheTest")?testId=" + testId;
                            var win = window.open(url, '_blank');
                            win.focus();
                        }
                    }
                    else {

                    var newTest = confirm("Are you sure you want to start this test?");
                    if(newTest == true)
                    {
                        var url = "@Url.Content("~/Test/TakeTheTest")?testId=" + testId;
                        var win = window.open(url, '_blank');
                        win.focus();
                        }
                    }
                }
            });
        });
    });

    $(document).ready(function () {

        // Initial page.
//        goToPage(1, @ViewBag.totalPage);

        $("#deactivated").click(function () {
            var con = confirm("Are you sure you want to deactivate this test?");
            if (con == true) {
                $.ajax({
                    url: "@Url.Content("~/Test/Deactivate")?ids=@Model.TestId" + "-",
                    type: 'GET',
                    success: function (data) {
                        if (data != null) {
                            alert(data);
                            window.location = "@Url.Content("~/Test/Details")?id=@Model.TestId";
                        }
                    }
                })
            }
        });

        $("#activated").click(function () {
            var con = confirm("Are you sure you want to activate this test?");
            if (con == true) {
                $.ajax({
                    url: "@Url.Content("~/Test/Activate")?ids=@Model.TestId" + "-",
                    type: 'GET',
                    success: function (data) {
                        if (data != null) {
                            alert(data);
                            window.location = "@Url.Content("~/Test/Details")?id=@Model.TestId";
                        }
                    }
                })
            }
        });

        $("#delete").click(function () {
            var con = confirm("Are you sure you want to delete this test?");
            if (con == true) {
                $.ajax({
                    url: "@Url.Content("~/Test/Delete")?ids=@Model.TestId" + "-",
                    type: 'GET',
                    success: function (data) {
                        if (data != null) {
                            alert(data);
                            window.location = "@Url.Content("~/Test/Index")?subjectId=@Model.SubjectId";
                        }
                    }
                })
            }
        });
    });

    function SendReport(id) {
        $("#report_content").html("<input type=\"hidden\" name=\"testId\" id=\"vio_questionId\" /> <div style=\"text-align: center\"> <input type=\"text\" name=\"message\" pattern=\".{5,100}\" required placeholder=\"Message\" title=\"The Message length is 5-100 characters.\" style=\"width: 330px\" /> <button type=\"submit\" value=\"Send\" class=\"btn\"> Send</button> </div>");
        $("#vio_questionId").val(id);
    }

</script>
<div class="site_map">
    @Html.ActionLink("Home", "Index", "Home") / @Html.ActionLink("Subjects", "Index", "Subject", new { }, null)
    / <span style="word-wrap: break-word">@Html.ActionLink(Model.Subject.Code, "Details", "Subject", new { id = Model.SubjectId }, null)</span>
    /
    @Html.ActionLink("Tests", "Index", "Test", new { subjectId = Model.SubjectId }, null)
    / <span style="word-wrap: break-word">@Model.Title</span>
</div>
<br />
<div class="main_nav">
    <span><a href="@Url.Content("~/Test/Index")?subjectId=@Model.SubjectId">
        <img src="@Url.Content("~/Images/back button.png")" style="height:30px"/></a></span>
</div>
<br />
@{
    if (!Model.IsActivated)
    {
    <div class="alert alert-error">
        <span>Deactivated.</span>
    </div>
    }
}
<table class="table table-bordered" style="table-layout: fixed">
    <tr>
        <td style="width: 150px; text-align: right">
            Title
        </td>
        <td>
            <span style="word-wrap: break-word">@Html.DisplayFor(model => model.Title)</span>
            @{
                if (Model.UserId != int.Parse(HttpContext.Current.User.Identity.Name))
                {
                <div style="display: inline-block; float: right">
                    <a href="#myModal" role="button" data-toggle="modal" class="vio_report" onclick="SendReport('@Model.TestId')">
                        <img src="@Url.Content("~/Images/report.gif")"/></a>
                </div>
                }
            }
        </td>
    </tr>
    <tr>
        <td style="text-align: right">
            Created by
        </td>
        <td>
            @Html.ActionLink(Model.User.FullName, "Index", "Account", new { userId = Model.UserId }, new { target = "_blank" })
        </td>
    </tr>
    <tr>
        <td style="text-align: right">
            Numbers of Questions
        </td>
        <td>
            @{
                <span>@Model.TestQuestions.Where(q => q.Question.IsActivated).Count()</span>
            }
        </td>
    </tr>
    <tr>
        <td style="text-align: right">
            Duration (minutes)
        </td>
        <td>
            @Html.DisplayFor(model => model.Duration)
        </td>
    </tr>
    @{
        if (Model.TestTypeId == 1)
        {
        <tr>
            <td style="text-align: right">
                Expires
            </td>
            <td>
                @Html.DisplayFor(model => model.Expires)
            </td>
        </tr>
        }
    }
    <tr>
        <td style="text-align: right">
        </td>
        <td>
            @{
                if (HttpContext.Current.User.IsInRole("1") || Model.UserId == int.Parse(HttpContext.Current.User.Identity.Name))
                {
                @Html.ActionLink("Edit", "Edit", new { id = @Model.TestId }) <span>|</span>
                <a href="#" id="delete">Delete</a> <span> |</span>
                }
                if (HttpContext.Current.User.IsInRole("1"))
                {
                    if (Model.IsActivated)
                    {
                <a href="#" id="deactivate">Deactivate</a>
                    }
                    else
                    {
                <a href="#" id="activate">Activate</a>
                    }
                <span>|</span>
                }
                
                <a href="#" onclick = "SetTestId('@Model.TestId')" class = "take_the_test">Take the
                    Test</a>
            }
        </td>
    </tr>
</table>
<div class="accordion" id="accordion2" style="margin: 0px">
    <div class="accordion-group" style="border: 0px">
        <div class="accordion-heading">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne"
                style="padding: 0px">>>More Details </a>
        </div>
        <div id="collapseOne" class="accordion-body collapse" style="border: 0px">
            <div class="accordion-inner" style="padding: 0px; padding-top: 5px; border: 0px">
                <table class="table table-bordered">
                    <tr>
                        <td style="width: 150px; text-align: right">
                            Created Time
                        </td>
                        <td>
                            @Html.DisplayFor(model => model.CreatedTime)
                        </td>
                    </tr>
                    <tr>
                        <td style="text-align: right">
                            Last Edited Time
                        </td>
                        <td>
                            @Html.DisplayFor(model => model.LastEditedTime)
                        </td>
                    </tr>
                    <td style="text-align: right">
                        Questions
                    </td>
                    <td>
                        @Html.ActionLink("Details", "TestQuestions", "Test", new { id = Model.TestId }, null)
                    </td>
                    <tr>
                        <td style="text-align: right">
                            Your Recent Tests
                        </td>
                        <td>
                            @Html.ActionLink("Details", "TestResults", "Test", new { id = Model.TestId }, null)
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>
<fieldset>
    <legend>Marks Chart</legend>
    <img src="@Url.Content("~/Test/CreateChart")?testId=@Model.TestId"/>
</fieldset>
<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
    aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
            ×</button>
        <h3 id="myModalLabel">
            Report</h3>
    </div>
    <div class="modal-body">
        @using (Ajax.BeginForm("Create", "Report", null, new AjaxOptions
        {
            InsertionMode = InsertionMode.Replace,
            HttpMethod = "GET",
            UpdateTargetId = "report_content",
        }, new
        {
            @class = "form-inline",
            id = "form_violation"
        }))
        {
            <div id="report_content">
                <input type="hidden" name="testId" id="vio_questionId" />
                <div style="text-align: center">
                    <input type="text" name="message" pattern=".{5,100}" required placeholder="Message"
                        title="The Message length is 5-100 characters." style="width: 330px" />
                    <button type="submit" value="Send" class="btn">
                        Send</button>
                </div>
            </div>
        }
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">
            Close</button>
    </div>
</div>
